// pages/functions/listinfo.js
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var config = require('../../config')
var util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchbarLoading: false,
    searchbarDesabled:false,
    busyinfo:false,
    endinfo:true,
    searchvalue:'',
    functype:'',
    funcname:'',
    menuflag:'2',//1发起2收藏
    querynum:10,//列表一次查询条数
    articles: [],//[{ title:'页面加载中，请稍后······'}],
    toAddUrl:'',
    toDetailUrl:'',
    toManageUrl:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      functype: this.options.functype,
      funcname: this.options.funcname
    })
    wx.setNavigationBarTitle({
      title: this.options.funcname
    });
    this.getSysParam();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    this.getList('up', '');

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getList('up', '');
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //this.getList();
    this.getList('down', this.data.searchvalue);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getSysParam() {
    var self = this
    qcloud.request({
      url: `${config.service.host}/weapp/getSysParams`,
      //method:'POST',
      data: { bigtype: 'SYSTEM', smalltype: 'MENU', seqid: self.data.functype},
      login: false,
      success(result) {
        if (result.data.data.status == '0') {
          self.setData({
            toDetailUrl: result.data.data.msg[0].def3,
            toAddUrl: result.data.data.msg[0].def4,
            toManageUrl: result.data.data.msg[0].def5,
          })
        } else {
          console.log(JSON.stringify(result.data.data));
        }
      },
      fail(error) {
        console.log('request fail', error);
      }
    })
  },
  //点击查询
  searchbarSubmit(e){
    var self=this;
    var searchvalue= e.detail.value.searchInput;
    self.setData({
      searchbarLoading: !self.data.searchbarLoading,
      searchbarDisabled: !self.data.searchbarDisabled,
      searchvalue: searchvalue
    })
    self.getList('up', searchvalue);
    //完成查询后再次设置可点击
    self.setData({
      searchbarLoading: !self.data.searchbarLoading,
      searchbarDisabled: !self.data.searchbarDisabled,
    })
  },
  //点击功能菜单
  menuClick(data){
    if (this.data.menuflag=='2'&&data.target.id=='self'){
      this.setData({
        menuflag: '1'
      })
      this.getList('up', '');
    } else if (this.data.menuflag == '1' && data.target.id == 'other'){
      this.setData({
        menuflag: '2'
      })
      this.getList('up', '');
    }
  },
  //获取信息列表
  getList:function(flag,search) {//flag:up顶部触发，down底部触发，search为空顶部重置search
    //console.log(flag,search)
    var self = this

    if(flag=='up'){
      self.setData({
        searchvalue: search, 
        busyinfo: false,
        endinfo: true,
        articles:[]
      })
    }
    var beginseq='';
    var articles = [];
    if(flag=='down'){
      self.setData({
        busyinfo: false,
        endinfo: true,
      })
      articles = self.data.articles;
      beginseq = articles[articles.length-1].seqid;
    }
    //console.log(beginseq,articles)
    var targeturl =self.data.toManageUrl;
    if(self.data.menuflag=='2'){
      targeturl = '/weapp/collectingManage'
    } else if (!targeturl){
      self.setData({
        endinfo: false,
        busyinfo: true
      })
      return
    }
    qcloud.request({
      url: `${config.service.host + targeturl}`,
      method:'POST',
      data: { opflag: '0', functype: self.data.functype, search: search, beginseq: beginseq, querynum:  self.data.querynum},
      login: true,
      success(result) {
        if (result.data.data.status == '0') {
          //console.log('return', result.data.data.msg)
          if (result.data.data.msg.length>0){
            articles = articles.concat(result.data.data.msg)
            self.setData({
              articles: articles,
              busyinfo: true
            })
          }else{
            self.setData({
              endinfo: false,
              busyinfo: true
            })
          }         
        } else {
          console.log(JSON.stringify(result.data.data));
          self.setData({
            busyinfo: true
          })
        }
      },
      fail(error) {
        console.log('request fail', error);
        util.relogin(self);
      }
    })
  },
  //新增功能
  bindAdd(){
    var self=this;
    var toAddUrl=self.data.toAddUrl;
    if (toAddUrl){
      toAddUrl = toAddUrl + (toAddUrl.indexOf('?') > -1? '&' : '?');
      wx.navigateTo({
        url: toAddUrl + 'functype=' + self.data.functype
      })
    }
  },
  toDetail(e){
    var openid=e.currentTarget.dataset.openid;
    var seqid = e.currentTarget.dataset.seqid;
    //console.log('/pages/functions/articledetail?functype=' + this.data.functype + '&openid=' + openid + '&seqid=' + seqid)
    //获取跳转页面参数，
    var self = this;
    var toDetailUrl = self.data.toDetailUrl;

    if (toDetailUrl) {
      toDetailUrl = toDetailUrl + (toDetailUrl.indexOf('?') > -1 ? '&' : '?');
    //完成跳转
    wx.navigateTo({
      url: toDetailUrl+'functype=' + this.data.functype+'&openid=' + openid + '&seqid=' + seqid
    })
    }
  },
})